package com.machine.action.order;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.OrderForm;
import com.machine.bean.PageBean;
import com.machine.service.order.OrderServer;
import com.opensymphony.xwork2.ActionSupport;

@Component("findOrderAction")
@Scope(value = "prototype")
public class FindOrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private OrderServer orderServer;
	private List<OrderForm> orders = null;
	private String providerName;
	private String buyName;
	
	private int page; // 第几页

	private PageBean pageBean; // 包含分布信息的bean

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getProviderName() {
		return providerName;
	}

	public void setProviderName(String providerName) {
		this.providerName = providerName;
	}

	public String getBuyName() {
		return buyName;
	}

	public void setBuyName(String buyName) {
		this.buyName = buyName;
	}

	public OrderServer getOrderServer() {
		return orderServer;
	}

	@Resource
	public void setOrderServer(OrderServer orderServer) {
		this.orderServer = orderServer;
	}

	public List<OrderForm> getOrders() {
		return orders;
	}

	public void setOrders(List<OrderForm> orders) {
		this.orders = orders;
	}

	@Override
	public String execute() throws Exception {
		
		if (page <= 0) {
			page = 1;
		}
		
		pageBean = orderServer.findOrder(providerName,buyName,page,2);
		orders = pageBean.getList();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		out
				.print("<form method='post' class='orderform' ><table class='back_table' cellspacing='0' cellpadding='0'>");
		out
				.print("<tr><th width='30'>选择</th><th>编号</th><th>订单状态</th><th>供应商编号</th><th>供应商名称</th><th>买家编号</th><th>买家名称</th><th>零件编号</th><th>零件数量</th><th>零件单价</th><th>零件总价</th><th>发布时间</th><th>附件信息</th></tr>");

		if (orders != null) {
			for (int i = 0; i < orders.size(); i++) {
				out.print("<tr>");
				out.print("<th width='20'><input name='id' value='"
						+ orders.get(i).getId() + "' type='checkbox'></th>");
				out.print("<th>");
				out.print(orders.get(i).getId());
				out.print("</th>");
				out.print("<th>");
				if (orders.get(i).getState() == 0) {
					out.print("等待处理");
				} else if (orders.get(i).getState() == 1) {
					out.print("处理完成");
				}
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getProviderid());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getProviderName());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getBuyid());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getBuyName());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getComponentid());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getAmount());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getUnitprice());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getAllprice());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getOuttime());
				out.print("</th>");
				out.print("<th>");
				out.print(orders.get(i).getInfo());
				out.print("</th>");
				out.print("</tr>");
			}
			
			out.print("<tr>");
			out.print("<td>");
			if(pageBean.getCurrentPage() == 1){
				out.print("上一页	首页");
			}else{
				out
				.print("<a onclick='return changeframe(1);'>首页</a>");

				out.print("<a onclick='return changeframe("+(pageBean.getCurrentPage() - 1)+");' >上一页</a>");
			}

			
			out.print("</td>");
			out.print("<td>");
			if(pageBean.getCurrentPage() != pageBean.getTotalPage()){
			
			out.print("<a onclick='return changeframe("+(pageBean.getCurrentPage() + 1)+");' >下一页</a>");

			out.print("<a onclick='return changeframe("+pageBean.getTotalPage()+");'>尾页</a>");
			}else{
				out.print("下一页	尾页");
			}

			out.print("共" + pageBean.getAllRow() + "条记录");
			out.print("</td>");
			out.print("</tr>");

		}
		out.print("</table>");
		out.print("</form>");
		out.flush();
		out.close();

		return SUCCESS;
	}

}
